package repo

import (
	"context"
	"gitlab.yoyiit.com/youyi/go-core/handler"
	"gitlab.yoyiit.com/youyi/go-core/repository"
	"gitlab.yoyiit.com/youyi/go-core/util"
)

type ProductCustomFieldListRowDBData struct {
	repository.BaseDBData
	ColumnId	int64	`orm:"column_id"`	//列表列ID
	JsonValue	string	`orm:"jsonValue"`	//JSON格式的值
	
}

type ProductCustomFieldListRowDBDataParam struct {
	ProductCustomFieldListRowDBData
	Ids       []int64
}

func (*ProductCustomFieldListRowDBData) TableName() string {
	return "product_custom_field_list_row"
}

func (param *ProductCustomFieldListRowDBData) getConditions() []*repository.Condition {
	return []*repository.Condition{
		repository.NewAndCondition(param),
	}
}

func (param *ProductCustomFieldListRowDBDataParam) listConditions() []*repository.Condition {
	var conditions []*repository.Condition

	if param.Ids != nil && len(param.Ids) > 0 {
    		conditions = append(conditions, repository.NewAndCondition("id in ?", param.Ids))
    }

	if param.ColumnId != 0 {		conditions = append(conditions, repository.NewAndCondition("column_id = ?", param.ColumnId))	}if param.ColumnId != 0 {		conditions = append(conditions, repository.NewAndCondition("column_id = ?", param.ColumnId))	}
	
	
	
	return conditions
}

type ProductCustomFieldListRowRepo interface {
	repository.BaseCommonRepo
	Get(context.Context, *ProductCustomFieldListRowDBData) (*ProductCustomFieldListRowDBData, error)
	Count(context.Context, *ProductCustomFieldListRowDBDataParam) (int64, error)
	List(context.Context, string, int32, int32, *ProductCustomFieldListRowDBDataParam) (*[]ProductCustomFieldListRowDBData, int64, error)
	SimpleList(ctx context.Context, order string, pageNum, pageSize int32, param *ProductCustomFieldListRowDBDataParam) (*[]ProductCustomFieldListRowDBData, int64, error)
}

type productCustomFieldListRowRepo struct {
	repository.BaseRepo
}

func (r *productCustomFieldListRowRepo) Get(ctx context.Context, param *ProductCustomFieldListRowDBData) (*ProductCustomFieldListRowDBData, error) {
	data, err := r.BaseGet(ctx, repository.NewQueryBuilder().Where(param.getConditions()))
	if data == nil {
		return nil, handler.HandleError(err)
	}
	return data.(*ProductCustomFieldListRowDBData), handler.HandleError(err)
}

func (r *productCustomFieldListRowRepo) List(ctx context.Context, order string, pageNum, pageSize int32, param *ProductCustomFieldListRowDBDataParam) (*[]ProductCustomFieldListRowDBData, int64, error) {
	data, count, err := r.BaseList(ctx, repository.NewListQueryBuilder(order, pageNum, pageSize).Where(param.listConditions()))
	if data == nil {
		return nil, count, handler.HandleError(err)
	}
	return data.(*[]ProductCustomFieldListRowDBData), count, handler.HandleError(err)
}


func (r *productCustomFieldListRowRepo) SimpleList(ctx context.Context, order string, pageNum, pageSize int32, param *ProductCustomFieldListRowDBDataParam) (*[]ProductCustomFieldListRowDBData, int64, error) {
	conditions := []string{}
	params := []interface{}{}


	if param.ColumnId != "" {
		             conditions = append(conditions, "column_id = ?")
		             params = append(params, param.ColumnId)
         }
	if param.JsonValue != "" {
		             conditions = append(conditions, "jsonValue = ?")
		             params = append(params, param.JsonValue)
         }
	

	var count int64
	err := r.Db.WithContext(ctx).Model(r.Model).Where(strings.Join(conditions, " and "), params...).Count(&count).Error
	if err != nil {
		return nil, 0, handler.HandleError(err)
	}
	if count == 0 {
		return nil, 0, nil
	}
	var result []ProductCustomFieldListRowDBData
	db := r.Db.WithContext(ctx).Where(strings.Join(conditions, " and "), params...)
	if order != "" {
		db = db.Order(order)
	}
	if pageNum > 0 && pageSize > 0 {
		db = db.Offset(int((pageNum - 1) * pageSize))
		db = db.Limit(int(pageSize))
	}
	err = db.Find(&result).Error
	return &result, count, handler.HandleError(err)
}


func (r *productCustomFieldListRowRepo) Count(ctx context.Context, param *ProductCustomFieldListRowDBDataParam) (int64, error) {
	count, err := r.BaseCount(ctx, repository.NewQueryBuilder().Where(param.listConditions()))
	return count, handler.HandleError(err)
}
